package ca.eloas.judging.rest.resources;

import ca.eloas.judging.messages.CompetitionListMessage;
import ca.eloas.judging.messages.CompetitionMessage;
import ca.eloas.judging.model.JudgeObject;
import ca.eloas.judging.rest.repositories.CompetitionRepository;
import ca.eloas.restsupport.operations.LIST;
import ca.eloas.restsupport.utils.BasicResource;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import static ca.eloas.judging.rest.operations.ChildLink.childLink;
import static ca.eloas.judging.rest.operations.CopyToMessage.copyToMessage;
import static ca.eloas.judging.rest.operations.SelfLink.selfLink;

/**
 * @author JP
 */
public class JudgeCompetitionResource extends BasicResource {


    private final JudgeObject judge;

    @Inject
    private ResourceFactory resourceFactory;

    @Inject
    private CompetitionRepository competitions;

    @AssistedInject
    public JudgeCompetitionResource(@Assisted JudgeObject j) {

        this.judge = j;
    }

    @Path("/")
    @LIST
    @GET
    public CompetitionListMessage listCompetitions() {

        return list(judge.getCompetitions(), CompetitionListMessage.class, CompetitionMessage.class).runOnList(
                selfLink()
        ).runOnItems(
                copyToMessage(),
                childLink("scores", "/scores"),
                selfLink()
        ).now();
    }

    @Path("/{id}")
    @GET
    public CompetitionMessage getCompetition(@PathParam("id") long id) {

        return get(competitions.get(id), CompetitionMessage.class).runAll(
                copyToMessage(),
                childLink("scores", "/scores"),
                selfLink()
        ).now();
    }

    @Path("/{id}/scores")
    public JudgeScoreResource getScores(@PathParam("id") long id) {

        return resourceFactory.createJudgeScoreResource(judge, competitions.get(id) );
    }
}
